package ca.cbc.localsrvextadmin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.Preparable;
import ca.cbc.giscommon.dao.CategoryDAO;
import ca.cbc.giscommon.dao.EventSubcategoryDAO;
import ca.cbc.giscommon.dao.SourceDAO;
import ca.cbc.giscommon.entity.Category;
import ca.cbc.giscommon.entity.EventSubcategory;
import ca.cbc.giscommon.entity.Source;
import ca.cbc.giscommon.util.HibernateUtil;
import ca.cbc.localsrvextadmin.util.ExceptionUtil;

public class ManageSourceAction extends LSEAction implements Preparable {

	private static final long serialVersionUID = -4971222587871222988L;
	private static Logger logger = LoggerFactory.getLogger(ManageSourceAction.class);
	
	private String actiontype = LIST;
	private List<Source> sourceList = null;
	private Source source = new Source();
	private List<Category> categoryList = null;
	private List<EventSubcategory> eventSubcategoryList = null;
	private Map<String, String> sourceTypeMap = null;
	


	public ManageSourceAction(){
		sourceTypeMap = new HashMap<String, String>();
		sourceTypeMap.put("content", "content");
		sourceTypeMap.put("event", "event");
		sourceTypeMap.put("twitter", "twitter");
		
	}
	
	public void prepare() throws Exception {
		categoryList = new CategoryDAO().list();		
		eventSubcategoryList = new EventSubcategoryDAO().list();
	}

	public String list() throws Exception  {
		setSourceList(new SourceDAO().listByOrder("id", true));
		
		actiontype = LIST;
		return actiontype;
	}
	public String edit() {
		return add();
	}
	public String add() {
		Source source;
		String id = getParameter("selectedSource");
		if(id != null) {
			SourceDAO dao = new SourceDAO();
			source = dao.findById(Long.valueOf(id));
		} else {
			source = new Source();
			source.setDigestFlag(true);
		}
		setSource(source);
		actiontype = ADD;
		return actiontype;
	}
	
	public String save() throws Exception {
		try {
			new SourceDAO().saveOrUpdate(source);
			HibernateUtil.flushAndClear();
		}
		catch (Exception e){
			addActionError("Saving feed failure: " + ExceptionUtil.getRootCauseMessage(e));
			logger.error("Saving feed failure: ", e);
			HibernateUtil.rollbackTransaction();
			return ERROR;
		}
		return SUCCESS;
	}
	
	// update digest status
	public String update() throws Exception {
		String id = getParameter("selectedSource");
		if(id != null) {
			SourceDAO dao = new SourceDAO();
			Source src = dao.findById(Long.valueOf(id));
			if(src != null) {
				src.setDigestFlag(!src.getDigestFlag());
				dao.saveOrUpdate(src);
			}
		}
		return SUCCESS;
	}

	public void setSourceList(List<Source> sourceList) {
		this.sourceList = sourceList;
	}

	public List<Source> getSourceList() {
		return sourceList;
	}
	
	public void setSource(Source source) {
		this.source = source;
	}

	public Source getSource() {
		return source;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public void setEventSubcategoryList(List<EventSubcategory> eventSubcategoryList) {
		this.eventSubcategoryList = eventSubcategoryList;
	}

	public List<EventSubcategory> getEventSubcategoryList() {
		return eventSubcategoryList;
	}
	
	public Map<String, String> getSourceTypeMap() {
		return sourceTypeMap;
	}

	public void setSourceTypeMap(Map<String, String> sourceTypeMap) {
		this.sourceTypeMap = sourceTypeMap;
	}
}
